package io.kiki.sba.registry.server.metadata.provide.data;

import io.kiki.sba.registry.common.model.Node;
import io.kiki.sba.registry.common.model.metaserver.ProvideDataChangeEvent;
import io.kiki.sba.registry.server.metadata.remoting.data.DefaultDataServerService;
import io.kiki.sba.registry.server.metadata.remoting.session.DefaultSessionServerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Set;

/**
 * <p>Provide Data is designed for two scenerio, as below: 1. Dynamic Configs inside
 * Sofa-Registry itself 2. Service Gaven (or say 'Watcher') are subscring messages through
 * Session-Server
 * <p>All above user cases stages a Config Center role by Sofa-Registry And all these infos are
 * madantorily persistenced to disk So, by leveraging meta server's JRaft feature, infos are
 * reliable and stable to be stored on MetaServer
 */
@Component
public class DefaultProvideDataNotifier implements ProvideDataNotifier {

    @Autowired
    private DefaultDataServerService defaultDataServerService;

    @Autowired
    private DefaultSessionServerService defaultSessionServerService;

    @Override
    public void notifyProvideDataChange(ProvideDataChangeEvent event) {
        Set<Node.NodeType> notifyTypes = event.getNodeTypes();
        if (notifyTypes.contains(Node.NodeType.data_store)) {
            defaultDataServerService.notifyProvideDataChange(event);
        }
        if (notifyTypes.contains(Node.NodeType.client_interface)) {
            defaultSessionServerService.notifyProvideDataChange(event);
        }
    }


    DefaultProvideDataNotifier setDataServerProvideDataNotifier(DefaultDataServerService defaultDataServerService) {
        this.defaultDataServerService = defaultDataServerService;
        return this;
    }


    DefaultProvideDataNotifier setSessionServerProvideDataNotifier(DefaultSessionServerService defaultSessionServerService) {
        this.defaultSessionServerService = defaultSessionServerService;
        return this;
    }
}
